﻿Imports Data


Public Class EmpleadosForm

#Region "Variables estaticas"
    Private id_empleado As Integer

#End Region

#Region "Enum"
    Private Enum Tipos_Modiciacion_Interfaz
        Ingreso = 1
        Modificacion = 2
        Eliminacion = 3
    End Enum

#End Region

    Private Sub EmpleadosForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Me.CargarListView()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CargarListView()
        Try
            Me.lv_empleado.Items.Clear()
            Using db As New SerigraphicaDBDataContext
                Dim l_empleado As New List(Of Empleados)
                l_empleado = db.Empleados.Select(Function(p) p).ToList

                lv_empleado.BeginUpdate()
                For Each item In l_empleado
                    lv_empleado.Items.Add(New ListViewItem(New String() {item.nombre, _
                                                                         item.apellido_paterno, _
                                                                         item.apellido_materno, _
                                                                         item.telefono_celular, _
                                                                         item.telefono_casa, _
                                                                         item.email, _
                                                                         item.Estado.nombre
                                                                         }) With {.Name = item.id})
                Next

                lv_empleado.EndUpdate()
                lv_empleado.Refresh()
            End Using
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.btnIngresar.Text = "Ingresar" Then
                Dim nuevo_empleado As New Empleados With {.nombre = txtNombre.Text, _
                                                          .apellido_paterno = txtApPaterno.Text, _
                                                          .apellido_materno = txtApMaterno.Text, _
                                                          .direccion = txtDireccion.Text, _
                                                          .email = txtEmail.Text, _
                                                          .fecha_ingreso = dtPicker.Value, _
                                                          .telefono_casa = CInt(txtTelfCasa.Text), _
                                                          .telefono_celular = CInt(txtTelfCelular.Text), _
                                                          .id_estado = 5, _
                                                          .usuario = Me.txtUsuario.Text, _
                                                          .password = Me.txtPassword.Text
                                                     }

                If (Me.Ingresar(nuevo_empleado)) Then
                    MsgBox("Ingresado con exito")
                    Me.CargarListView()
                    Me.ModificarIngresarInterfaz()
                End If
            Else
                If (Me.Modificar()) Then
                    MsgBox("Modificado con exito")
                    Me.CargarListView()
                    Me.ModificarIngresarInterfaz()
                End If
            End If
            
        Catch ex As Exception

        End Try
    End Sub
    Private Function Modificar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim empleado As Empleados = db.Empleados.Select(Function(p) p).Where(Function(p) p.id = id_empleado).FirstOrDefault

                empleado.nombre = Me.txtNombre.Text
                empleado.apellido_paterno = Me.txtApPaterno.Text
                empleado.apellido_materno = Me.txtApMaterno.Text
                empleado.direccion = Me.txtDireccion.Text
                empleado.telefono_casa = Me.txtTelfCasa.Text
                empleado.telefono_celular = Me.txtTelfCelular.Text
                empleado.email = Me.txtEmail.Text
                empleado.fecha_ingreso = Me.dtPicker.Value
                empleado.usuario = Me.txtUsuario.Text
                empleado.password = Me.txtPassword.Text
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False

        End Try
    End Function



    Private Function Ingresar(ByVal empleado As Empleados) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                db.Empleados.InsertOnSubmit(empleado)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Try
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub


    Private Sub EditarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        Try
            Me.ModificarInterfazEditar(lv_empleado.SelectedItems(0).Name)
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarInterfazEditar(ByVal id As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.id_empleado = id
                Dim empleado As Empleados = db.Empleados.Select(Function(p) p).Where(Function(p) p.id = id_empleado).FirstOrDefault

                Me.txtNombre.Text = empleado.nombre
                Me.txtApPaterno.Text = empleado.apellido_paterno
                Me.txtApMaterno.Text = empleado.apellido_materno
                Me.txtDireccion.Text = empleado.direccion
                Me.txtTelfCasa.Text = empleado.telefono_casa
                Me.txtTelfCelular.Text = empleado.telefono_celular
                Me.txtEmail.Text = empleado.email
                Me.dtPicker.Value = empleado.fecha_ingreso
                Me.txtUsuario.Text = empleado.usuario
                Me.txtPassword.Text = empleado.password

                Me.btnIngresar.Text = "Modificar"
            End Using
            Return True
        Catch ex As Exception
            Return False

        End Try
    End Function

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Try
            Me.ModificarIngresarInterfaz()
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarIngresarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.txtApPaterno.Text = String.Empty
            Me.txtApMaterno.Text = String.Empty
            Me.txtDireccion.Text = String.Empty
            Me.txtTelfCasa.Text = String.Empty
            Me.txtTelfCelular.Text = String.Empty
            Me.txtEmail.Text = String.Empty
            Me.dtPicker.Value = Date.Now

            Me.txtUsuario.Text = String.Empty
            Me.txtPassword.Text = String.Empty

            Me.btnIngresar.Text = "Ingresar"
            Me.txtNombre.Focus()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


   

    Private Sub ContextMenuStrip1_Opening(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        Try
            Using db As New SerigraphicaDBDataContext
                Me.id_empleado = lv_empleado.SelectedItems(0).Name
                Dim empleado As Empleados = db.Empleados.Where(Function(p) p.id = id_empleado).Select(Function(p) p).FirstOrDefault
                If (empleado.id_estado <> 5) Then
                    Me.EstadoToolStripMenuItem.Text = "Habilitar"
                Else
                    Me.EstadoToolStripMenuItem.Text = "Inhabilitar"
                End If

            End Using
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EstadoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EstadoToolStripMenuItem.Click
        Try
            If Me.ModificarEstado() Then
                MsgBox("Estado Modificado Satisfactoriamente", MsgBoxStyle.OkCancel)
                Me.CargarListView()
            Else
                MsgBox("Error")
            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Function ModificarEstado() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim empleado As Empleados = db.Empleados.Where(Function(p) p.id = id_empleado).Select(Function(p) p).FirstOrDefault
                If empleado.id_estado = 5 Then
                    empleado.id_estado = 6
                Else
                    empleado.id_estado = 5
                End If

                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class